package com.swg.controller.backend;

import com.swg.common.Constants;
import com.swg.common.ServerResponse;
import com.swg.pojo.User;
import com.swg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @Author 【swg】.
 * @Date 2018/1/3 12:02
 * @DESC
 * @CONTACT 317758022@qq.com
 */
@Controller
@RequestMapping("/manage/user/")
public class UserManageController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> login(String username, String password, HttpSession session){
        ServerResponse<User> response = userService.login(username,password);
        if(response.isSuccess()){
            User user = response.getData();
            if(user.getRole() == Constants.Role.ROLE_ADMIN){
                session.setAttribute(Constants.CURRENT_USER,user);
                return response;
            }else {
                return ServerResponse.createByErrorMessage("不是管理员，无法登陆");
            }
        }
        return response;
    }
}
